package com.easylive.web.controller;

import com.easylive.annotation.GlobalInterceptor;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.CommentTopTypeEnum;
import com.easylive.entity.enums.PageSize;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.enums.UserActionTypeEnum;
import com.easylive.entity.po.UserAction;
import com.easylive.entity.po.VideoComment;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.UserActionQuery;
import com.easylive.entity.query.VideoCommentQuery;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.VideoCommentResultVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserActionService;
import com.easylive.service.VideoCommentService;
import com.easylive.service.VideoInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comment")
@Validated
@Slf4j
public class VideoCommentController extends ABaseController {
    @Resource
    private VideoCommentService videoCommentService;
    @Resource
    private UserActionService userActionService;
    @Resource
    private VideoInfoService videoInfoService;

    @RequestMapping("/postComment")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO postComment(@NotEmpty String videoId,
                                  @NotEmpty @Size(max = 500) String content,
                                  Integer replyCommentId,
                                  @Size(max = 50) String imgPath) {
        VideoComment videoComment = new VideoComment();
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoComment.setVideoId(videoId);
        videoComment.setUserId(tokenUserInfoDto.getUserId());
        videoComment.setAvatar(tokenUserInfoDto.getAvatar());
        videoComment.setNickName(tokenUserInfoDto.getNickName());
        videoComment.setContent(content);
        videoComment.setImgPath(imgPath);
        videoCommentService.postComment(videoComment,replyCommentId);
        return getSuccessResponseVO(videoComment);


    }

    @RequestMapping("/loadComment")
    public ResponseVO loadComment(@NotEmpty String videoId,//TODO
                                  Integer pageNo,
                                  Integer orderType) {
        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(videoId);
        if(videoInfo==null){
            throw new BusinessException((ResponseCodeEnum.CODE_404));
        }
        if(videoInfo.getInteraction() != null && videoInfo.getInteraction().contains(Constants.ZERO.toString())){
            return getSuccessResponseVO(new ArrayList<>());
        }
        VideoCommentQuery query = new VideoCommentQuery();
        query.setVideoId(videoId);
        query.setPageNo(pageNo);
        query.setLoadChildren(true);
        query.setPageSize(PageSize.SIZE15.getSize());
        query.setpCommentId(0);
        String orderBy = orderType == null || orderType == 0 ? "like_count desc,comment_id desc" : "comment_id desc";
        query.setOrderBy(orderBy);
        PaginationResultVO<VideoComment> commentList = videoCommentService.findListByPage(query);

        if(pageNo == null || pageNo == 1){
            List<VideoComment> topCommentList = topCommentUtil(videoId);
            if(!topCommentList.isEmpty()){
                List<VideoComment> commentData = commentList.getList().stream().filter(item -> !item.getCommentId().equals(topCommentList.get(0).getCommentId())).collect(Collectors.toList());
                commentData.addAll(0,topCommentList);
                commentList.setList(commentData);
            }
        }

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        List<UserAction> userActionList = new ArrayList<>();
        if(tokenUserInfoDto != null){
            UserActionQuery userActionQuery = new UserActionQuery();
            userActionQuery.setUserId(tokenUserInfoDto.getUserId());
            userActionQuery.setVideoId(videoId);
            userActionQuery.setActionTypeArray(new Integer[]{UserActionTypeEnum.COMMENT_LIKE.getType(),UserActionTypeEnum.COMMENT_HATE.getType()});
            userActionList = userActionService.findListByParam(userActionQuery);
        }
        VideoCommentResultVO videoCommentResultVO = new VideoCommentResultVO();
        videoCommentResultVO.setCommentData(commentList);
        videoCommentResultVO.setUserActionList(userActionList);
        System.out.println(videoCommentResultVO.getCommentData());
        return getSuccessResponseVO(videoCommentResultVO);


    }

    public List<VideoComment> topCommentUtil(String videoId){
        VideoCommentQuery query = new VideoCommentQuery();
        query.setVideoId(videoId);
        query.setTopType(CommentTopTypeEnum.TOP.getType());
        query.setLoadChildren(true);
        return videoCommentService.findListByParam(query);
    }

    @RequestMapping("/topComment")
    public ResponseVO topComment(@NotNull Integer commentId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoCommentService.topComment(commentId,tokenUserInfoDto.getUserId());

        return getSuccessResponseVO(null    );
    }

    @RequestMapping("/cancelTopComment")
    public ResponseVO cancelTopComment(@NotNull Integer commentId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoCommentService.cancelTopComment(commentId,tokenUserInfoDto.getUserId());

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/userDelComment")
    public ResponseVO userDelComment(@NotNull Integer commentId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoCommentService.deleteComment(commentId,tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(null);
    }


}
